package br.dcc.ufmg.controller;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import br.dcc.ufmg.domain.Class;
import br.dcc.ufmg.domain.Method;
import br.dcc.ufmg.domain.Package;
import br.dcc.ufmg.view.FilterResultJFrame;

public class FilterResultController {

	private final FilterResultJFrame frame = new FilterResultJFrame(this);

	public void populateTableMethods(List<Method> methods, int totalSize) {
		frame.getLabelTotalResults().setText("" + methods.size() + " of " + totalSize);
		DefaultTableModel model = (DefaultTableModel) frame.getTableArtifacts().getModel();
		model.addColumn("Method");
		model.addColumn("Source");
		model.addColumn("Package");
		for (Method method : methods) {
			model.addRow(new String[] { method.getName(),
					method.getClass_().getSource(),
					method.getClass_().getPackage_().getName() });
		}
	}

	public void populateTableClasses(List<Class> classes, int totalSize) {
		frame.getLabelTotalResults().setText("" + classes.size() + " of " + totalSize);
		DefaultTableModel model = (DefaultTableModel) frame.getTableArtifacts().getModel();
		model.addColumn("Class");
		model.addColumn("Source");
		model.addColumn("Package");
		for (Class class_ : classes) {
			model.addRow(new String[] { class_.getName(), class_.getSource(),
					class_.getPackage_().getName() });
		}
	}

	public void toExcel(JTable table, File file) {
		try {
			TableModel model = table.getModel();
			FileWriter excel = new FileWriter(file);

			for (int i = 0; i < model.getColumnCount(); i++) {
				excel.write(model.getColumnName(i) + ";");
			}

			excel.write("\n");

			for (int i = 0; i < model.getRowCount(); i++) {
				for (int j = 0; j < model.getColumnCount(); j++) {
					excel.write(model.getValueAt(i, j).toString() + ";");
				}
				excel.write("\n");
			}

			excel.close();

		} catch (IOException e) {
			System.out.println(e);
		}
	}

	public void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
		frame.getCsvChooser().setFileFilter(new FileFilter() {
			@Override
			public boolean accept(File file) {
				return file.getAbsolutePath().endsWith(".csv");
			}

			@Override
			public String getDescription() {
				return "Only CSV files are accepted.";
			}
		});
		int returnVal = frame.getCsvChooser().showOpenDialog(frame);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			try {
				File csv = frame.getCsvChooser().getSelectedFile();
				csv.createNewFile();
				toExcel(frame.getTableArtifacts(), csv);
			} catch (IOException ex) {
				Logger.getLogger(FilterResultJFrame.class.getName()).log(
						Level.SEVERE, null, ex);
			}

		}
	}// GEN-LAST:event_jButton1ActionPerformed

	public void populateTablePackages(List<Package> packages, int totalSize) {
		frame.getLabelTotalResults().setText("" + packages.size() + " of " + totalSize);
		DefaultTableModel model = (DefaultTableModel) frame.getTableArtifacts().getModel();
		model.addColumn("Package");
		for (Package package_ : packages) {
			model.addRow(new String[] { package_.getName() });
		}
	}

	public FilterResultJFrame getFrame() {
		return frame;
	}
	
}
